﻿
CREATE FUNCTION [dbo].[f_convert_to_rur]
	(
    @p_amount_cur decimal(16,4),
	@p_date datetime,
	@p_nsi_currency_id int
	)
RETURNS decimal(16,4)
AS
	BEGIN
	DECLARE @m_rate decimal(8,4)
	SET @m_rate = 1
	IF (SELECT TOP 1 SHORT_NAME FROM NSI_CURRENCY WHERE ID = @p_nsi_currency_id) != 'RUB'
	BEGIN
		SELECT @m_rate = RATE 
		FROM [dbo].[NSI_EXCHANGE_RATE] 
			WHERE NSI_CURRENCY_ID = @p_nsi_currency_id 
			AND [DATE] = CAST(@p_date as DATE)
	END
	RETURN @p_amount_cur*@m_rate
	END